//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TridentWordAddIn.Common;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.ResourceHelper;
using Microsoft.TridentWordAddIn.Server;

namespace Microsoft.TridentWordAddIn.Presentation
{
    internal class ServerBroker : IServerBroker
    {
        private UserData userData;
        private IController controller;

        /// <summary>
        /// Initializes a new instance of the <see cref="ServerBroker"/> class.
        /// </summary>
        /// <param name="userData">The user data.</param>
        public ServerBroker(UserData userData)
        {
            this.userData = userData;
            this.controller = Controller.Instance(null);
        }

        /// <summary>
        /// Determines whether [is connection password required] [the specified connection].
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <returns>
        /// 	<c>true</c> if [is connection password required] [the specified connection]; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsConnectionPasswordRequired(TridentConnection connection)
        {
            if (connection.ConnectionType == ConnectionType.Sql)
            {
                if (connection.SqlParameters.IsTrusted)
                {
                    return false;
                }

                return connection.SqlParameters.Password.IsNullOrEmpty();
            }

            return true;
        }

        /// <summary>
        /// Gets the verified server.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <returns>ITridentServer</returns>
        public ITridentServer GetVerifiedServer(TridentConnection connection)
        {
            if (connection == null)
            {
                return null;
            }

            ITridentServer tridentServer = this.GetTridentServerByConnection(connection);
            ConnectionFailedException connException = null;
            if (TridentServer.TestConnection(tridentServer.CurrentTridentConnection, ref connException) == false)
            {   
                TridentConnection tridentConnection = tridentServer.CurrentTridentConnection;
                if (this.controller.ShowServerConnectionForm(
                    ref tridentConnection,
                    true                                        
                    ))
                {
                    tridentServer.CurrentTridentConnection = tridentConnection;
                }
                else
                {
                    tridentServer = null;
                }
            }

            return tridentServer;
        }

        /// <summary>
        /// Gets the gene pattern server for connection.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <returns>ITridentServer</returns>
        private ITridentServer GetTridentServerByConnection(TridentConnection connection)
        {
            ITridentServer tridentServer = new TridentServer();
            tridentServer.CurrentTridentConnection = connection;
            if (IsConnectionPasswordRequired(connection))
            {
                foreach (TridentConnection storedConnection in this.userData.Connections.Values)
                {
                    if (storedConnection.ConnectionType == connection.ConnectionType)
                    {
                        if (connection.ConnectionType == ConnectionType.Sql)
                        {
                            if (connection.SqlParameters.ServerName.Compare(storedConnection.SqlParameters.ServerName) &&
                                connection.SqlParameters.IsTrusted == storedConnection.SqlParameters.IsTrusted &&
                                connection.SqlParameters.UserName.Compare(storedConnection.SqlParameters.UserName))
                            {
                                tridentServer.CurrentTridentConnection = storedConnection;
                                break;
                            }
                        }
                        if (connection.ConnectionType == ConnectionType.WebService)
                        {
                            if (connection.WebServiceParameters.Url.Compare(storedConnection.WebServiceParameters.Url) &&
                                 connection.WebServiceParameters.IsAnonymous == storedConnection.WebServiceParameters.IsAnonymous &&
                                 connection.WebServiceParameters.UserName.Compare(storedConnection.WebServiceParameters.UserName))
                            {
                                tridentServer.CurrentTridentConnection = storedConnection;
                                break;
                            }
                        }
                    }
                }
            }

            return tridentServer;
        }
    }
}
